﻿using AutoMapper;
using ERPSystem.Common.Domains.Department;
using ERPSystem.Common.Domains.Material;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace BasicInformationWrite.API.BasicInformationCommons.Materialss
{
    public class MaterialCascadeCreateHandler:IRequestHandler<MaterialCascadeCreateCommand,Result>
    {
        private readonly IBaseRepository<MaterialCascade> materialCascadebaseRepository;
        private readonly IMapper mapper;

        public MaterialCascadeCreateHandler(IBaseRepository<MaterialCascade> materialCascadebaseRepository, IMapper mapper)
        {
            this.materialCascadebaseRepository = materialCascadebaseRepository;
            this.mapper = mapper;
        }



        /// <summary>
        ///物料分类添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(MaterialCascadeCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            try
            {
                MaterialCascade matercascsde = mapper.Map<MaterialCascade>(request);
                matercascsde.CreatedOwnerId = "张三";
                matercascsde.CreatedTime = DateTime.Now;
                var res = await materialCascadebaseRepository.Add(matercascsde);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
